﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using KPIS.GERP.GHRMS.DAL;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.IBLL; 
using KPIS.DBM;

using KPIS.GERP.GHRMS.DALFactory;

namespace KPIS.GERP.GHRMS.BLL
{
    public  class MoveRequestMainBLL

    {
        DBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);
     

            // strSQL = " insert into hrm_es_move_requests(emp_seq,approved_status,record_status,created_by,created_when)";
            //strSQL += " values(" + lblEmpSeq.Text + ",'" + SystemInfo ._ProcessRecordStatus + "','" + SystemInfo._ActiveRecordStatus + "',1," + DateTime.Today + ");" + "\n";


        public void Insert_hrm_es_move_requests(int emp_seq, string ref_no, int approve_seq, string approved_pos, string remarks, int created_by)
        {

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(9);
                dbManager.AddParameters(0, "@emp_seq", emp_seq);
                dbManager.AddParameters(1, "@approved_position", approved_pos);
                dbManager.AddParameters(2, "@remarks", remarks);
                dbManager.AddParameters(3, "@record_status", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(4, "@created_by", created_by);
                dbManager.AddParameters(5, "@approved_status", SystemInfo._ProcessRecordStatus);
                dbManager.AddParameters(6, "@approved_seq", approve_seq);
                dbManager.AddParameters(7, "@created_when", DateTime.Today);
                dbManager.AddParameters(8, "@ref_no", ref_no);


                string strSQL;
                strSQL = " INSERT INTO HRM_ES_MOVE_REQUESTS "
                    + " (ref_no,approved_seq,emp_seq,approved_position,remarks,record_status,created_by,approved_status,created_when)"
                 + " values(@ref_no,@approved_seq,@emp_seq,@approved_position,@remarks,@record_status,@created_by,@approved_status,@created_when)";


                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                

            }
            catch (Exception ex)
            {
                throw new Exception("");
            }
            finally
            {
                dbManager.Dispose();
            }

        }
        public void Update_hrm_es_move_requests(int req_seq, string ref_no, int approved_seq, string approved_position, string remarks, int updated_by)
        {
         


            try
            {
                dbManager.Open();
                dbManager.CreateParameters(7);
                dbManager.AddParameters(0, "@approved_position", approved_position);
                dbManager.AddParameters(1, "@remarks", remarks);
                dbManager.AddParameters(2, "@updated_by", updated_by);
                dbManager.AddParameters(3, "@req_seq", req_seq);
                dbManager.AddParameters(4, "@approved_seq", approved_seq);    
                dbManager.AddParameters(5, "@updated_when", DateTime.Today);
                dbManager.AddParameters(6, "@ref_no", ref_no);
                string strSQL;
                strSQL = " update hrm_es_move_requests set "
                + " approved_position = approved_position , "
                + " remarks = @remarks , "
                + " approved_seq = @approved_seq , "
                + " updated_by = @updated_by , "
                + " updated_when = @updated_when ,"
                + " ref_no = @ref_no "
                + " where req_seq= @req_seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
  
            }
            catch
            {
                throw new Exception("");
            }
            finally
            {
                dbManager.Dispose();
            }
        }
        public void Delete(int req_seq,int UpdatedBy)
        {
            try
            {

                dbManager.Open();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@record_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@updated_by", UpdatedBy);
                dbManager.AddParameters(2, "@updated_when",DateTime.Today  );
                dbManager.AddParameters(3, "@req_seq", req_seq);

                string sql;
                sql = " update hrm_es_move_requests set "
                + " record_status=@record_status,"
                + " updated_by = @updated_by,"
                + " updated_when = @updated_when "
                + " where req_seq = @req_seq";

                dbManager.ExecuteNonQuery(CommandType.Text, sql);
  
            }
            catch (Exception ex)
            {
                throw new Exception("");
            }
            finally
            {
                dbManager.Dispose();
            }
        }



        public DataTable select(int emp_seq)
        {

            try
            {
                string sql;
                sql = " select a.*,b.record_status_name " 
                + " from hrm_es_move_requests a,mas_record_statuses b "
                + " where a.record_status='U'"
                + " and a.approved_status = b.record_status"
                + " and a.emp_seq = " + emp_seq;
                DataSet ds;
                DataTable dt;
                ds = new DataSet();
                dbManager.Open();
                ds = dbManager.ExecuteDataSet(CommandType.Text, sql);
                dt = ds.Tables[0];
               
                return dt;
            }
            catch (Exception ex)
            {

                return null;
                // throw new Exception("");
            }
            finally
            {
                dbManager.Dispose();
            }
        }


        public int get_req_seq(int emp_seq)
        {
            string sql;
            sql = "select max(req_seq) from hrm_es_move_requests where emp_seq=" + emp_seq;
            try
            {
                object obj;
                dbManager.Open();
                obj = dbManager.ExecuteScalar(CommandType.Text, sql);
               
                return (int)obj + 1;
            }
            catch
            {
                return 1;
                // throw new Exception("");
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public int getSeq(int emp_seq)
        {
            string sql;
            sql = "select max(seq) from hrm_es_move_request_dtls where emp_seq=" + emp_seq ;
            try
            {
                object obj;
                dbManager.Open();
                obj = dbManager.ExecuteScalar(CommandType.Text, sql);
             
                return (int)obj + 1;
                
            }
            catch (Exception Ex)
            {
                return 1;
                // throw new Exception("");
            }
            finally
            {
                dbManager.Dispose();
            }
        }



    }
}
